//
//  WZOfficeController.m
//  ETong
//
//  Created by wanzhao on 15/10/28.
//  Copyright (c) 2015年 wanzhao. All rights reserved.
//

#import "WZOfficeController.h"
#import "WZTabBarButton.h"
#import "WZAddressContactController.h"
#import "WZDayPlanController.h"
#import "WZMobileSignController.h"
#import "WZMessageController.h"
#import "WZProcedureController.h"
#import "WZTrajectoryController.h"
#import "WZOffMapController.h"
#import "WZOutManagerController.h"
#import "WZUserInfoController.h"
#import "CFolderMgr.h"
#import "CContactModel.h"
#import "CContactMgr.h"
#import "WZRootViewController.h"
#import "MBProgressHUD+WZ.h"
#import "UIBarButtonItem+category.h"
#import "MBProgressHUD+WZ.h"
#import "WZTaskManagerController.h"

typedef enum
{
    MenuSignBtnTag = 3000,
    MenuDayPlanBtnTag,
    MenuAddressContactBtnTag,
    MenuOffMapBtnTag,
    MenuTrajectoryBtnTag,
    MenuMessageBtnTag,
    MenuProcedureBtnTag,
    MenuPluBtnTag,
    MenuOutTag,
    MenuTaskManagerTag
    
}MenuBtnTag;


#define buttonWidth  HARDWARE_SCREEN_WIDTH / 2.0f
#define buttonHeigt  (HARDWARE_SCREEN_WIDTH / 18.0f) * 5.0f


@interface WZOfficeController ()


//菜单视图
@property (nonatomic, weak) UIView *menueView;
@property (nonatomic, strong) NSMutableArray *menuButtonArray;
@property (nonatomic, weak) UIScrollView *scrollView;
@property (nonatomic, weak) UIImageView *introView;

/**OA userId*/
@property (nonatomic, copy) NSString *oaUserId;

@end

@implementation WZOfficeController


- (NSMutableArray *)menuButtonArray
{
    if (_menuButtonArray == nil) {
        _menuButtonArray = [NSMutableArray array];
    }
    return _menuButtonArray;
}
- (void)dealloc
{
    NSLog(@"WZOfficeController dealloc");
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self getUserInfor];
    
    [self setupUI];
    
    [self addHeaderViews];
    
    [self addMenueButton];
    
}


- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = NO;
    if (IsIOS7) {
        [self.navigationController.navigationBar setBarTintColor:RGBCOLOR(44, 170, 253)];
    } else {
        [self.navigationController.navigationBar setTintColor:RGBCOLOR(44, 170, 253)];
    }
}


- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
//    [[NSNotificationCenter defaultCenter] postNotificationName:REMOVEGUESTURE object:nil];
}

#pragma mark -- 检测手机是否被注册
- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
//    [[NSNotificationCenter defaultCenter] postNotificationName:ADDGUESTURE object:nil];
    
    self.oaUserId = [USER_DEFAULT objectForKey:@"oaUserId"];
    
    NSString *bundleID = [NSBundle mainBundle].bundleIdentifier;
    NSString *uuid = [SSKeychain passwordForService:bundleID account:@"DeviceUUID"];
    
    if (_oaUserId && uuid) {
        
        [self checkMoblieAuthorize];
    }

}

- (void)lookPersonalInformation
{
    
    UINavigationController *nav = (UINavigationController *)[UIApplication sharedApplication].keyWindow.rootViewController;
     nav.navigationBarHidden = NO;
    self.navigationController.navigationBarHidden = YES;
    
    WZUserInfoController *info = [[WZUserInfoController alloc] init];
    [nav pushViewController:info animated:YES];
}

- (UIView *)addLine
{
    UIView *rowLine1 = [[UIView alloc] init];
    rowLine1.frame = CGRectMake(0, buttonHeigt, self.view.width, 0.5);
    rowLine1.backgroundColor = RGBCOLORWITH16COLOR(0xc4c6c5);
    [self.menueView addSubview:rowLine1];
    return rowLine1;
}

#pragma mark -- 创建button
- (WZTabBarButton *)addButtonWithTitle:(NSString *)title norImageName:(NSString *)norImageName
{
    WZTabBarButton *button = [WZTabBarButton buttonWithType:UIButtonTypeCustom];
    if (title.length > 0) {
        [button setTitle:title forState:UIControlStateNormal];
    }
    [button setTitleColor:RGBCOLOR(153, 153, 153) forState:UIControlStateNormal];
    if (norImageName.length > 0) {
        [button setImage:[UIImage imageNamed:norImageName] forState:UIControlStateNormal];
    }
    
    button.contentEdgeInsets = UIEdgeInsetsMake(10, 0, 0, 0);
    button.titleLabel.font = Font14;
    [button addTarget:self action:@selector(menuBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.menueView addSubview:button];
    [self.menuButtonArray addObject:button];
    return button;
}

#pragma mark -- ButtonDidClick
- (void)menuBtnClick:(WZTabBarButton *)btn
{
    UINavigationController *nav = (UINavigationController *)[UIApplication sharedApplication].keyWindow.rootViewController;
    nav.navigationBarHidden = NO;
    self.navigationController.navigationBarHidden = YES;
    
    NSString *mobileStr = [USER_DEFAULT stringForKey:ISMOBILEAVALable];
    
        switch (btn.tag) {
        case MenuSignBtnTag:
        {
            NSString *mobileStr = @"已认证";
            if ([mobileStr isEqualToString:@"已认证"]) {
                WZMobileSignController *sign = [[WZMobileSignController alloc] init];
                [nav pushViewController:sign animated:YES];
            } else if (M_ISSTRNULL(mobileStr)){
                nav.navigationBarHidden = YES;
                self.navigationController.navigationBarHidden = NO;
                [MBProgressHUD showError:@"手机未认证"];
            } else {
                 nav.navigationBarHidden = YES;
                self.navigationController.navigationBarHidden = NO;
                [MBProgressHUD showError:mobileStr];
            }

        }
            break;
        case MenuDayPlanBtnTag:
        {
            WZDayPlanController *dayPlan = [[WZDayPlanController alloc] init];
            [nav pushViewController:dayPlan animated:YES];
        }
            break;
        case MenuAddressContactBtnTag:
        {
            WZAddressContactController *add = [[WZAddressContactController alloc] init];
            [nav pushViewController:add animated:YES];
        }
            break;
        case MenuOffMapBtnTag:
        {
            WZOffMapController *offMap = [[WZOffMapController alloc] init];
            [nav pushViewController:offMap animated:YES];
        }
            break;
        case MenuTrajectoryBtnTag:
        {
            WZTrajectoryController *tra = [[WZTrajectoryController alloc] init];
            [nav pushViewController:tra animated:YES];
        }
            break;
        case MenuMessageBtnTag:
        {
            WZMessageController *msg = [[WZMessageController alloc] init];
            [nav pushViewController:msg animated:YES];
        }
            break;
        case MenuProcedureBtnTag:
        {
            WZProcedureController *pro =[[WZProcedureController alloc] init];
            [nav pushViewController:pro animated:YES];
        }
            break;
        case MenuOutTag:
        {
//            NSString *mobileStr = @"已认证";
            if ([mobileStr isEqualToString:@"已认证"]) {
                
                WZOutManagerController *outVc = [[WZOutManagerController alloc] init];
                [nav pushViewController:outVc animated:YES];
            } else if (M_ISSTRNULL(mobileStr)){
                 nav.navigationBarHidden = YES;
                self.navigationController.navigationBarHidden = NO;
                [MBProgressHUD showError:@"手机未认证"];
            } else {
                 nav.navigationBarHidden = YES;
                self.navigationController.navigationBarHidden = NO;
                [MBProgressHUD showError:mobileStr];
            }

        }
            break;
                
        case MenuTaskManagerTag:
        {
            
//            NSString *mobileStr = @"已认证";
            if ([mobileStr isEqualToString:@"已认证"]) {
                
                WZTaskManagerController *taskVc = [[WZTaskManagerController alloc] init];
                [nav pushViewController:taskVc animated:YES];
            } else if (M_ISSTRNULL(mobileStr)){
                nav.navigationBarHidden = YES;
                self.navigationController.navigationBarHidden = NO;
                [MBProgressHUD showError:@"手机未认证"];
            } else {
                nav.navigationBarHidden = YES;
                self.navigationController.navigationBarHidden = NO;
                [MBProgressHUD showError:mobileStr];

            }
        }
            break;
         
        default:
                
            break;
    }
}

- (void)setupUI {
    
    self.view.backgroundColor = WhiteColor;
    if (IsIOS7) {
        self.edgesForExtendedLayout = UIRectEdgeNone;
    }
    
    //    self.view.frame = CGRectMake(0, 64, self.view.width, HARDWARE_SCREEN_HEIGHT - 64 - 49);
    
    //    self.navigationItem.rightBarButtonItem = [UIBarButtonItem BarButtonItemWithImageName:@"title" title:nil target:self sel:@selector(lookPersonalInformation) titleFont:Font18 titleColor:ClearColor];
    self.navigationItem.title = @"办公中心";

}


- (void)addHeaderViews {
    
    UIScrollView *scrollView = [[UIScrollView alloc] init];
    scrollView.backgroundColor = ClearColor;
    scrollView.showsVerticalScrollIndicator = NO;
    scrollView.frame = CGRectMake(0, 0, self.view.width, HARDWARE_SCREEN_HEIGHT - 64 - 49);
    [self.view addSubview:scrollView];
    self.scrollView = scrollView;
    
    UIImageView *introView = [[UIImageView alloc] init];
    introView.image = [UIImage imageNamed:@"office_center_img_normal"];
    introView.frame = CGRectMake(0, 0, self.view.width, introView.image.size.height);
    [scrollView addSubview:introView];
    self.introView = introView;
    
    
    
    UIView *menueVIew = [[UIView alloc] init];
    menueVIew.backgroundColor = ClearColor;
    menueVIew.frame = CGRectMake(0, CGRectGetMaxY(introView.frame), self.view.width, buttonHeigt * 3);
    [scrollView addSubview:menueVIew];
    self.menueView = menueVIew;
    
    scrollView.contentSize = CGSizeMake(0, CGRectGetMaxY(menueVIew.frame));
    if (CGRectGetMaxY(menueVIew.frame) > HARDWARE_SCREEN_HEIGHT - 64 - 49) {
        scrollView.scrollEnabled = YES;
    } else {
        scrollView.scrollEnabled = NO;
    }
}


#pragma mark -- 宫格按钮设置
- (void)addMenueButton {
    
    WZTabBarButton *signBtn = [self addButtonWithTitle:@"手机打卡" norImageName:@"phone_sign"];
    signBtn.tag = MenuSignBtnTag;
    
    WZTabBarButton *dayPlanBtn = [self addButtonWithTitle:@"工作日程" norImageName:@"work_schedule"];
    dayPlanBtn.tag = MenuDayPlanBtnTag;
    
//    WZTabBarButton *addressContactBtn = [self addButtonWithTitle:@"通讯录" norImageName:@"address_book"];
//    addressContactBtn.tag = MenuAddressContactBtnTag;
    
    
    WZTabBarButton *procedureBtn = [self addButtonWithTitle:@"流程办理" norImageName:@"process_management"];
    procedureBtn.tag = MenuProcedureBtnTag;
    
    
    WZTabBarButton *offMapBtn = [self addButtonWithTitle:@"离线地图" norImageName:@"offline_map"];
    offMapBtn.tag = MenuOffMapBtnTag;
    
//    WZTabBarButton *trajectoryBtn = [self addButtonWithTitle:@"轨迹跟踪" norImageName:@"trajectory_tracking"];
//    trajectoryBtn.tag = MenuTrajectoryBtnTag;
    
//    WZTabBarButton *messageBtn = [self addButtonWithTitle:@"消息中心" norImageName:@"message_center" ];
//    messageBtn.tag = MenuMessageBtnTag;
    
    
    WZTabBarButton *outBtn = [self addButtonWithTitle:@"外出管理" norImageName:@"out_manager"];
    outBtn.tag = MenuOutTag;
    
    WZTabBarButton *taskBtn = [self addButtonWithTitle:@"任务管理" norImageName:@"address_book"];
    taskBtn.tag = MenuTaskManagerTag;
    
    __block CGFloat btnx = 0;
    __block CGFloat btny = 0;
    
    [self.menuButtonArray enumerateObjectsUsingBlock:^(WZTabBarButton *obj, NSUInteger idx, BOOL *stop) {
        btnx = (idx % 2) * buttonWidth;
        btny = (idx / 2) * buttonHeigt;
        obj.frame = CGRectMake(btnx, btny, buttonWidth, buttonHeigt);
        
    }];
    
    
    UIView *rowLine1 = [self addLine];
    rowLine1.frame = CGRectMake(0, buttonHeigt, self.view.width, 0.5);
    
    UIView *rowLine2 = [self addLine];
    rowLine2.frame = CGRectMake(0, buttonHeigt * 2, self.view.width, 0.5);
    
    UIView *rowLine3 = [self addLine];
    rowLine3.frame = CGRectMake(0, buttonHeigt * 3, self.view.width, 0.5);
    
    UIView *line1 = [self addLine];
    line1.frame = CGRectMake(buttonWidth, 0, 0.5, buttonHeigt * 3);
    
    UIView *line2 = [self addLine];
    line2.frame = CGRectMake(buttonWidth * 2, 0, 0.5, buttonHeigt * 3);
}

- (void)getUserInfor {
    
    NSString *oaUserId = [USER_DEFAULT objectForKey:@"oaUserId"];
    self.oaUserId = oaUserId;
}


// 检查手机认证
- (void)checkMoblieAuthorize {
    
    [NetWorkShare checkMobileWithUserId:self.oaUserId successHandle:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        [MBProgressHUD hideHUDForView:nil];
        
        NSString *mobileStr = @"";
        if (responseObject) {
            NSString *status =  [NSString stringWithFormat:@"%@",responseObject[@"status"]];
            if ([[responseObject objectForKey:@"success"] boolValue]) { // 0 代表没有认证
                
                if ([status isEqualToString:@"2"]) {
                    mobileStr = @"已认证";
                    WZLog(@"%@", mobileStr);
                } else {
                    
                }
                
            } else {
                
                if ([status isEqualToString:@"-1"]) {
                    
//                    [MBProgressHUD showError:responseObject[@"message"]];
                    WZLog(@"没有对应的用户");
                } else if ([status isEqualToString:@"0"]) {
//                    [MBProgressHUD showError:responseObject[@"message"]];
                    WZLog(@"手机未认证");
                } else if ([status isEqualToString:@"1"]) {
                    mobileStr = @"待审核";
                    WZLog(@"待审核");
                } else if ([status isEqualToString:@"3"]) {
                    mobileStr = @"审核被拒绝";
                    WZLog(@"审核被拒绝");
                }
                
            }
            
        } else {
            
        }
        
        [USER_DEFAULT setObject:mobileStr forKey:ISMOBILEAVALable];
        [USER_DEFAULT synchronize];
        
    } failureHandle:^(AFHTTPRequestOperation *operation, NSError *error) {
        [MBProgressHUD hideHUDForView:nil];
        
    }];
}

@end
